package com.fitness.fitness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.convertor.CoachConvertor;
import com.fitness.fitness.convertor.CourseScheduleConvertor;
import com.fitness.fitness.convertor.VenueConvertor;
import com.fitness.fitness.mapper.CoachMapper;
import com.fitness.fitness.mapper.CourseScheduleMapper;
import com.fitness.fitness.mapper.VenueMapper;
import com.fitness.fitness.modal.coach.entity.Coach;
import com.fitness.fitness.modal.coach.vo.CoachVO;
import com.fitness.fitness.modal.course.entity.CourseCategory;
import com.fitness.fitness.modal.course.entity.CourseSchedule;
import com.fitness.fitness.modal.course.params.CourseSchedulePageQueryParams;
import com.fitness.fitness.modal.course.vo.CourseScheduleVO;
import com.fitness.fitness.modal.resource.entity.Venue;
import com.fitness.fitness.modal.resource.vo.VenueVO;
import com.fitness.fitness.service.ICourseScheduleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CourseScheduleServiceImpl extends ServiceImpl<CourseScheduleMapper,CourseSchedule> implements ICourseScheduleService {

    private final CoachMapper coachMapper;
    private final VenueMapper venueMapper;

    private final CoachConvertor coachConvertor;
    private final VenueConvertor venueConvertor;

    private final CourseScheduleConvertor courseScheduleConvertor;

    @Override
    public List<CoachVO> queryAvailableCoach(String startTime, String endTime) {
        List<Coach> coaches = coachMapper.queryAvailableCoach(startTime,endTime);
        return coachConvertor.entities2Vos(coaches);
    }

    @Override
    public List<VenueVO> queryAvailableVenue(String startTime, String endTime) {
        List<Venue> venues = venueMapper.queryAvailableVenue(startTime,endTime);
        return venueConvertor.entities2Vos(venues);
    }

    @Override
    public CourseScheduleVO getCourseScheduleByCourseId(Long courseId) {
        QueryWrapper<CourseSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<CourseSchedule> list = this.baseMapper.selectList(queryWrapper);
        if(list == null || list.isEmpty()){
            return null;
        }
        CourseSchedule courseSchedule = list.get(0);
        return courseScheduleConvertor.entity2Vo(courseSchedule);
    }

    @Override
    public Page<CourseScheduleVO> getCourseSchedulePage(CourseSchedulePageQueryParams queryParams) {
        // 参数构建
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        Page<CourseSchedule> page = new Page<>(pageNum, pageSize);

        // 查询数据
        Page<CourseSchedule> coachBoPage = this.baseMapper.getCourseSchedulePage(page, queryParams);
        return courseScheduleConvertor.entity2Vo(coachBoPage);
    }

    @Override
    public boolean deleteCourseSchedule(Long id) {
        return false;
    }

    @Override
    public List<CourseScheduleVO> getCourseSchedulesByCourseId(Long courseId) {
        QueryWrapper<CourseSchedule> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        List<CourseSchedule> list = this.baseMapper.selectList(queryWrapper);
        return courseScheduleConvertor.entities2Vos(list);
    }

    @Override
    public CourseScheduleVO getCourseScheduleById(Long id) {
        return courseScheduleConvertor.entity2Vo(this.baseMapper.selectById(id));
    }

    @Override
    public List<String> getCourseScheduleStartDate(Long coachId, String yearMonth) {
        return this.baseMapper.getCourseScheduleStartDate(coachId,yearMonth);
    }

    @Override
    public List<CourseScheduleVO> getCourseScheduleDetail(String date) {
        return courseScheduleConvertor.entities2Vos(this.baseMapper.getCourseScheduleDetail(date));
    }
}
